package com.hutiao.snowhb.common;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeUtil {
	
	public static Date currentTime() {
		return new Date(System.currentTimeMillis());
	}
	
	public static String currentTimeStr() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date curr = new Date(System.currentTimeMillis());
		return df.format(curr);
	}
	
	public static String longToDateString(long longTime){
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
		return sdf.format(longTime);
	}
	
	/**
	 * 将日期转换为字符串
	 * @param date		要转换为String的日期
	 * @param format	日期格式
	 * @return
	 */
	public static String Date2String(Date date, String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		String time = df.format(date);
		return time;
	}
	
	
	public static String Date2String(Date date) {
		return DateTimeUtil.Date2String(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String dateToWeek(Date date){
		final String[] weekArra = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
		int iw = date.getDay();
		return weekArra[iw];
	}
	/**
	 * String 类型转化 Date类型   格式 yyyy-MM-dd
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date String2Date(String strDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date String2Date(String strDate, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	
	/**
	 * String 类型转化 Date类型   格式 yyyy-MM-dd HH:mm:ss
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date String2Date2(String strDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static String string2Week(String dt){
		Date date = String2Date(dt);
		return dateToWeek(date);
	}
	
	public static String Timestamp2Week(Timestamp ts){
		final String[] weekArra = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		int iw = ts.getDay();
		return weekArra[iw];
	}
	
	
	/**
	 * 
	 * @param d
	 * @param day
	 * @return
	 * @throws ParseException
	 */
	public static Date addDate(Date d, long day) throws ParseException {

		long time = d.getTime();
		day = day * 24 * 60 * 60 * 1000;
		time += day;
		return new Date(time);

	}
	
	/**
	 * date1 - data2 的时间差（分钟）
	 * @param date1
	 * @param date2
	 */
	public static long getTimeDifference(Date date1, Date date2) {
		
		long difference = date1.getTime() - date2.getTime();
		long mins = difference / (1000 * 60);
		
		return mins;
	}
	
	
	
	public static void main(String[] args) {
//		String startTime = "2013-01-01 00:00:00";
//		try {
//			String prestartTime = DateTimeUtil.Date2String(DateTimeUtil.addDate(DateTimeUtil.String2Date2(startTime), -1), "yyyy-MM-dd HH:mm:ss");
//			
//			
//			System.out.println(prestartTime);
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
		String tradeNo = "XYJPSK149FLY130582256949382";
		String orderId = (tradeNo.length()-15<=0) ? tradeNo : tradeNo.substring(tradeNo.length()-15, tradeNo.length());
		
		System.out.println(tradeNo);
		System.out.println(orderId);
		
	}
}
